/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package lyra;

import java.util.List;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;
import javax.swing.ListSelectionModel;
import javax.swing.table.AbstractTableModel;
import lyra.pojos.*;

/**
 *
 * @author Koustas, Marmaridis, Mariolou
 */
public final class FMusicGroup extends javax.swing.JInternalFrame {

    Musicgroup musicgroup;
    Musicgroup OriginalMusicGroup;
    List<Object[]> curMusicGroupArtist;
    List<Object[]> curAvailableArtists;
    boolean bChanges = false;
    boolean formLoaded = false;
    boolean bFormClosing = false;
    boolean addArtistsVisible = false;
    
    /**
     * Creates new form frmArtist
     */
   private FListMusicGroup  parentForm;

   SimpleDateFormat sDF = new SimpleDateFormat("dd/MM/yyyy");
   
   public class MusicGroupArtistTableModel extends AbstractTableModel 
   {
   
       private List<Object[]> vwMusicGroupArtistList;
           
       private final String[] columnNames = new String[] {
                "MusicGroupID", "ArtistID", "Όνομα", "Επώνυμο", "Καλλιτεχνικό Όνομα"
        };
        private final Class[] columnClass = new Class[] {
            Long.class, Long.class, String.class, String.class, String.class
        };
 
        public MusicGroupArtistTableModel(List<Object[]> MusicGroupArtistList)
        {
           this.vwMusicGroupArtistList = MusicGroupArtistList;
        }

        public void removeRow(int row) {
            vwMusicGroupArtistList.remove(row);
        }

        public void addRow(Object[] newRow) {
            vwMusicGroupArtistList.add(newRow);
        }

        @Override
        public String getColumnName(int column)
        {
            return columnNames[column];
        }
 
        @Override
        public Class<?> getColumnClass(int columnIndex)
        {
            return columnClass[columnIndex];
        }

        @Override
        public int getColumnCount()
        {
            return columnNames.length;
        }

        @Override
        public int getRowCount()
        {
            return vwMusicGroupArtistList.size();
        }
        
        @Override
        public boolean isCellEditable(int row, int col) {
            return false;
        }
                
        @Override
        public Object getValueAt(int rowIndex, int columnIndex)
        {
            try {
                Object[] row = (Object[]) this.vwMusicGroupArtistList.get(rowIndex);
                return row[columnIndex];
            } catch (Exception ex) {
                    ex.printStackTrace(System.out);
                    return null;
            }

        }
   }

    private class AvailableArtistTableModel extends AbstractTableModel 
    {
   
        private final List<Object[]> vwAvailableArtists;
           
        private final String[] columnNames = new String[] {"ArtistID", "Όνομα", "Επώνυμο", "Καλλιτεχνικό Όνομα"};
        private final Class[] columnClass = new Class[] {Long.class, String.class, String.class, String.class};
 
        public AvailableArtistTableModel(List<Object[]> vwAvailableArtists)
        {
           this.vwAvailableArtists = vwAvailableArtists;
        }
     
        @Override
        public String getColumnName(int column)
        {
            return columnNames[column];
        }
 
        @Override
        public Class<?> getColumnClass(int columnIndex)
        {
            return columnClass[columnIndex];
        }

        @Override
        public int getColumnCount()
        {
            return columnNames.length;
        }

        @Override
        public int getRowCount()
        {
            return vwAvailableArtists.size();
        }
        
        @Override
        public boolean isCellEditable(int row, int col) {
            return false;
        }
        
        @Override
        public Object getValueAt(int rowIndex, int columnIndex)
        {
            try {
                Object[] row = (Object[]) this.vwAvailableArtists.get(rowIndex);
                return row[columnIndex];
            } catch (Exception ex) {
                    ex.printStackTrace(System.out);
                    return null;
            }

        }
   }

    
   private void fillMusicGroupArtist(long musicgroupID) {
       
        curMusicGroupArtist = new ArrayList();
        
        if (musicgroupID == -1) {
            Query qMusicGroupArtist =  LYRA.e.createNativeQuery("SELECT mga.* FROM ADMIN1.vwMusicGroupArtist mga WHERE 1=2");
            curMusicGroupArtist = (List<Object[]>)qMusicGroupArtist.getResultList();
        } else {
            Query qMusicGroupArtist =  LYRA.e.createNativeQuery("SELECT mga.* FROM ADMIN1.vwMusicGroupArtist mga WHERE mga.MusicGroupID = " + musicgroupID);
            curMusicGroupArtist =  (List<Object[]>)qMusicGroupArtist.getResultList();
        }
        
        MusicGroupArtistTableModel model = new MusicGroupArtistTableModel(curMusicGroupArtist);
        
        tblMusicGroupArtists.setModel(model);

        this.tblMusicGroupArtists.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        
        for (int intCol=0;intCol<=1;intCol++) {
            if (tblMusicGroupArtists.getColumnModel().getColumnCount() > intCol) {
                tblMusicGroupArtists.getColumnModel().getColumn(intCol).setMinWidth(0);
                tblMusicGroupArtists.getColumnModel().getColumn(intCol).setMaxWidth(0);
            }
        }
        this.tblMusicGroupArtists.setCellSelectionEnabled(false); 
        this.tblMusicGroupArtists.setRowSelectionAllowed(true); 
        
    }
   
    public FMusicGroup(FListMusicGroup parent) {

        initComponents();
        
        this.parentForm = parent;
        
        this.setTitle("Εισαγωγή στοιχείων συγκροτήματος");
        Insertion();
        dtFormationDate.setFormats("dd/MM/yyyy");
        setLockCtrls(false);
        fillMusicGroupArtist(-1);
        formLoaded = true;
        setSearchAvailableSongs(false);
        LYRA.setFrameIcon(this,"WindowsGroups_7309.png");
        parentForm.EnableDisableForm(false);
    }
    
    public FMusicGroup(FListMusicGroup parent, Musicgroup mg ) {

        OriginalMusicGroup = mg;
        initComponents();
        musicgroup = OriginalMusicGroup;
        this.parentForm = parent;
        
        this.setTitle("Μεταβολή στοιχείων συγκροτήματος");
        dtFormationDate.setFormats("dd/MM/yyyy");
        setLockCtrls(false);
        formLoaded = true;
        fillMusicGroupArtist(mg.getMusicgroupid());
        setSearchAvailableSongs(false);
        LYRA.setFrameIcon(this,"WindowsGroups_7309.png");
        parentForm.EnableDisableForm(false);
    }
    
    private void setSearchAvailableSongs(boolean showhide) {
        tblAvailableArtists.setVisible(showhide);
        btnAddSelectedArtistToMusicGroup.setVisible(showhide);
        jScrollPane4.setVisible(showhide);
        lblAvailableArtists.setVisible(showhide);
        if (showhide) {
            btnAddMusicGroupArtist.setText("Ακύρωση Εισαγωγής");
            this.setSize(this.getWidth(), 560);
        } else {
            btnAddMusicGroupArtist.setText("Εισαγωγή Καλλιτέχνη");
            this.setSize(this.getWidth(), 360);
        }
    }

    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        LYRAPUEntityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("LYRAPU").createEntityManager();
        lyra.pojos.Musicgroup musicgroup = OriginalMusicGroup;
        tbarManage = new javax.swing.JToolBar();
        tbbModify = new javax.swing.JButton();
        tbbInsert = new javax.swing.JButton();
        tbbDelete = new javax.swing.JButton();
        tbbExit = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtName = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        dtFormationDate = new org.jdesktop.swingx.JXDatePicker();
        jLabel7 = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        tblMusicGroupArtists = new javax.swing.JTable();
        btnAddMusicGroupArtist = new javax.swing.JButton();
        btnDeleteMusicGroupArtist = new javax.swing.JButton();
        lblAvailableArtists = new javax.swing.JLabel();
        jScrollPane4 = new javax.swing.JScrollPane();
        tblAvailableArtists = new javax.swing.JTable();
        btnAddSelectedArtistToMusicGroup = new javax.swing.JButton();

        setClosable(true);
        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Συγκρότημα");
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameClosing(evt);
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
            }
        });

        tbarManage.setFloatable(false);
        tbarManage.setRollover(true);

        tbbModify.setText("Αποθήκευση");
        tbbModify.setFocusable(false);
        tbbModify.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        tbbModify.setPreferredSize(new java.awt.Dimension(50, 50));
        tbbModify.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        tbbModify.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tbbModifyActionPerformed(evt);
            }
        });
        tbarManage.add(tbbModify);

        tbbInsert.setText("Εισαγωγή");
        tbbInsert.setFocusable(false);
        tbbInsert.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        tbbInsert.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        tbbInsert.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tbbInsertActionPerformed(evt);
            }
        });
        tbarManage.add(tbbInsert);

        tbbDelete.setText("Διαγραφή");
        tbbDelete.setFocusable(false);
        tbbDelete.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        tbbDelete.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        tbbDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tbbDeleteActionPerformed(evt);
            }
        });
        tbarManage.add(tbbDelete);

        tbbExit.setText("Έξοδος");
        tbbExit.setFocusable(false);
        tbbExit.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        tbbExit.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        tbbExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tbbExitActionPerformed(evt);
            }
        });
        tbarManage.add(tbbExit);

        jPanel1.setBackground(new java.awt.Color(204, 255, 255));

        jLabel1.setText("Επωνυμία Συγκροτήματος:");

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, musicgroup, org.jdesktop.beansbinding.ELProperty.create("${name}"), txtName, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        txtName.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtNameKeyTyped(evt);
            }
        });

        jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        jLabel5.setText("Ημερομηνία δημιουργίας:");

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, musicgroup, org.jdesktop.beansbinding.ELProperty.create("${formationdate}"), dtFormationDate, org.jdesktop.beansbinding.BeanProperty.create("date"));
        bindingGroup.addBinding(binding);

        dtFormationDate.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                dtFormationDatePropertyChange(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, 232, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(dtFormationDate, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(161, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(dtFormationDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jLabel7.setBackground(new java.awt.Color(51, 255, 51));
        jLabel7.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel7.setText("Λίστα Καλλιτεχνών");

        tblMusicGroupArtists.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane3.setViewportView(tblMusicGroupArtists);

        btnAddMusicGroupArtist.setText("Εισαγωγή Καλλιτέχνη");
        btnAddMusicGroupArtist.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddMusicGroupArtistActionPerformed(evt);
            }
        });

        btnDeleteMusicGroupArtist.setText("Διαγραφή Καλλιτέχνη");
        btnDeleteMusicGroupArtist.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteMusicGroupArtistActionPerformed(evt);
            }
        });

        lblAvailableArtists.setBackground(new java.awt.Color(51, 255, 51));
        lblAvailableArtists.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblAvailableArtists.setText("Πίνακας Διαθέσιμων Καλλιτεχνών");

        tblAvailableArtists.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        tblAvailableArtists.getTableHeader().setReorderingAllowed(false);
        jScrollPane4.setViewportView(tblAvailableArtists);

        btnAddSelectedArtistToMusicGroup.setText("Προσθήκη Επιλεγμένου Καλλιτέχνη στη Λίστα Καλλιτεχνών");
        btnAddSelectedArtistToMusicGroup.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddSelectedArtistToMusicGroupActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane3)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 557, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addContainerGap())
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(btnAddSelectedArtistToMusicGroup)
                                .addGap(130, 130, 130))))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(btnAddMusicGroupArtist)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(btnDeleteMusicGroupArtist))
                            .addComponent(tbarManage, javax.swing.GroupLayout.PREFERRED_SIZE, 354, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 541, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblAvailableArtists, javax.swing.GroupLayout.PREFERRED_SIZE, 557, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(0, 0, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(tbarManage, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnAddMusicGroupArtist)
                    .addComponent(btnDeleteMusicGroupArtist))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblAvailableArtists)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnAddSelectedArtistToMusicGroup)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void StartEdit()
    {
        setLockCtrls(false);
        //setChanges(false);
    }
            
    private void FinisEdit()
    {
        setLockCtrls(true);

        tbbModify.setEnabled(true);
        //tbbModify.setIcon("imgUpdate.png");
    }
    
    private void setLockCtrls(boolean lock)
    {
        if (lock) 
        {
            tbbModify.setText("Μεταβολή");
            tbbExit.setText("Έξοδος");
        }
        else
        {
            tbbModify.setText("Αποθήκευση");
            tbbExit.setText("Ακύρωση");
        }
        
        tbbModify.setEnabled(bChanges);
        tbbInsert.setEnabled(lock);
        tbbDelete.setEnabled(lock);
    
        txtName.setEditable(!lock);
        dtFormationDate.setEditable(!lock);
    }
    
    private boolean isDataValid()
    {
        //Έλεγχος ύπαρξης Επωνύμου & Ονόματος
        if ( (txtName.getText().trim()).length() == 0 )
        {
            LYRA.MsgBox("Πρέπει να δοθεί επωνυμία συγκροτήματος υποχρεωτικά", "ΠΡΟΣΟΧΗ");
            txtName.setFocusable(true);
            return false;
        }

        if (curMusicGroupArtist.size() < 2) {
             LYRA.MsgBox("Ένα συγκρότημα πρέπει να περιέχει τουλάχιστον 2 καλλιτέχνες", "ΠΡΟΣΟΧΗ");
             return false;
        }
        
        //αν φτάσει ως εδώ τα δεδομένα είναι ΟΚ
        return true;
    }
            
    private void tbbModifyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tbbModifyActionPerformed
        UpdateMusicGroup();
    }//GEN-LAST:event_tbbModifyActionPerformed

    private void tbbInsertActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tbbInsertActionPerformed
        Insertion();
    }//GEN-LAST:event_tbbInsertActionPerformed

    private void tbbDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tbbDeleteActionPerformed
        DeleteMusicGroup();
    }//GEN-LAST:event_tbbDeleteActionPerformed

    private void  DeleteMusicGroup() {
        //Διαγραφή συγκροτήματος
        if ( LYRA.yesno("Θέλετε σίγουρα να διαγραφεί το συγκρότημα " +  musicgroup.getName().trim()) == 0 )
        {
            try {
                EntityTransaction et = null;
                et = LYRA.e.getTransaction();
                et.begin();
                
                Query queryDelete =  LYRA.e.createNativeQuery("DELETE FROM ADMIN1.MusicGroup a WHERE a.MusicGroupID = " + musicgroup.getMusicgroupid().toString());
                queryDelete.executeUpdate();

                et.commit();
                parentForm.RefreshMusicGroupTable();
                //Ενεργοποίηση της προηγούμενης φόρμας
                parentForm.EnableDisableForm(true);
                //Κλείσιμο φόρμας
                dispose();
            }
            catch (Exception e) {
                LYRA.MsgBox(e.getMessage(),"ΠΡΟΣΟΧΗ");
            }
        }
    }
    
    private boolean CloseForm() {
        if (!tbbExit.isEnabled()) return false;
        
        if (tbbExit.getText()=="Έξοδος")
        {
            LYRA.e.clear();
            parentForm.RefreshMusicGroupTable();
            parentForm.setEnabled(true);
            return true;
        }
        else
        {
            if (bChanges)
            {
                if ( LYRA.yesno("Είστε σίγουροι ότι θέλετε να χαθούν οι αλλαγές; ") == 0 )
                {
                    LYRA.e.clear();
                    parentForm.RefreshMusicGroupTable();
                    return true;
                } else {
                    FinisEdit();
                }
             } else {
                return true;
            }
        }        
        return false;
    }
    
    private void tbbExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tbbExitActionPerformed
        if (CloseForm()) {
            bFormClosing = true;
            parentForm.EnableDisableForm(true);
            dispose();
        }
    }//GEN-LAST:event_tbbExitActionPerformed

    private void txtNameKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtNameKeyTyped
        setChanges(true);
    }//GEN-LAST:event_txtNameKeyTyped

    private void dtFormationDatePropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_dtFormationDatePropertyChange
        if ((evt.getPropertyName() == "date") && (formLoaded)) {
            setChanges(true);
        }
    }//GEN-LAST:event_dtFormationDatePropertyChange

    private void btnAddMusicGroupArtistActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddMusicGroupArtistActionPerformed
        addArtistsVisible=!addArtistsVisible;
        setSearchAvailableSongs(addArtistsVisible);
        if (addArtistsVisible) fillAvailableMusicGroupArtists();
    }//GEN-LAST:event_btnAddMusicGroupArtistActionPerformed

    private void fillAvailableMusicGroupArtists() {
       
        curAvailableArtists = new ArrayList();
        
        Query qAvailableArtists = LYRA.e.createNativeQuery("SELECT art.ArtistID, art.FirstName, art.LastName, art.ArtisticName FROM ADMIN1.Artist art");
        curAvailableArtists = (List<Object[]>)qAvailableArtists.getResultList();
        
        AvailableArtistTableModel model = new AvailableArtistTableModel(curAvailableArtists);
        
        tblAvailableArtists.setModel(model);
        this.tblAvailableArtists.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
          
        if (tblAvailableArtists.getColumnModel().getColumnCount() > 0) {
            tblAvailableArtists.getColumnModel().getColumn(0).setMinWidth(0);
            tblAvailableArtists.getColumnModel().getColumn(0).setMaxWidth(0);
        }

        this.tblAvailableArtists.setCellSelectionEnabled(false); 
        this.tblAvailableArtists.setRowSelectionAllowed(true); 

    }
   
    
    private void btnDeleteMusicGroupArtistActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteMusicGroupArtistActionPerformed
        if (tblMusicGroupArtists.getSelectedRowCount() > 0) {
            int TableRowToDelete = tblMusicGroupArtists.getSelectedRow();
            ((MusicGroupArtistTableModel)tblMusicGroupArtists.getModel()).removeRow(TableRowToDelete);
            ((MusicGroupArtistTableModel)tblMusicGroupArtists.getModel()).fireTableRowsDeleted(TableRowToDelete, TableRowToDelete);
            setChanges(true);
        } else {
            LYRA.MsgBox("Επιλέξτε πρώτα καλλιτέχνη προς διαγραφή", "ΠΡΟΣΟΧΗ");
        }
    }//GEN-LAST:event_btnDeleteMusicGroupArtistActionPerformed

    private void btnAddSelectedArtistToMusicGroupActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddSelectedArtistToMusicGroupActionPerformed

        if (tblAvailableArtists.getSelectedRowCount() > 0 ) {
            int TableRowToAdd = tblAvailableArtists.getSelectedRow();

            for (int curRow=0; curRow<tblMusicGroupArtists.getRowCount(); curRow++) {
                if (((Long)tblMusicGroupArtists.getValueAt(curRow, 1)).equals((Long)tblAvailableArtists.getValueAt(TableRowToAdd, 0))) {
                    LYRA.MsgBox("Ο συγκεκριμένος καλλιτέχνης υπάρχει ήδη στο συγκρότημα!", "ΠΡΟΣΟΧΗ");
                    return;
                }
            }

           ((MusicGroupArtistTableModel)tblMusicGroupArtists.getModel()).addRow(new Object[] {musicgroup.getMusicgroupid(),
                tblAvailableArtists.getValueAt(TableRowToAdd, 0),
                tblAvailableArtists.getValueAt(TableRowToAdd, 1),
                tblAvailableArtists.getValueAt(TableRowToAdd, 2),
                tblAvailableArtists.getValueAt(TableRowToAdd, 3)});
        ((MusicGroupArtistTableModel)tblMusicGroupArtists.getModel()).fireTableRowsInserted(tblMusicGroupArtists.getRowCount(), tblMusicGroupArtists.getRowCount());
        setChanges(true);

        }

    }//GEN-LAST:event_btnAddSelectedArtistToMusicGroupActionPerformed

    private void formInternalFrameClosing(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameClosing
        if (!bFormClosing) {
            if (CloseForm()) {
                bFormClosing = true;
                parentForm.EnableDisableForm(true);
                dispose();
            }
        } else {
            parentForm.EnableDisableForm(true);
            dispose();
        }        
    }//GEN-LAST:event_formInternalFrameClosing

    private void UpdateMusicGroup() {

        if (tbbModify.getText()=="Μεταβολή")
        {
            StartEdit();
        }
        else
        {
            if (isDataValid())
            {
                
                EntityTransaction et = null;
                
                
                try {
                    
                    Query mQueryArtist = LYRA.e.createQuery("SELECT art FROM Artist art");
                    Collection<Artist> art = (Collection<Artist>) mQueryArtist.getResultList();

                    et = LYRA.e.getTransaction();
                    et.begin();
                    
                    if (musicgroup.getMusicgroupartistCollection()!= null) {
                        Query queryDelete = LYRA.e.createNativeQuery("DELETE FROM ADMIN1.MusicGroupArtist mga WHERE mga.MusicGroupID = " + musicgroup.getMusicgroupid().toString());
                        queryDelete.executeUpdate();
                    }
                    
                    if (musicgroup.getMusicgroupartistCollection() == null) {
                        Collection<Musicgroupartist> emptyCollection = new ArrayList<Musicgroupartist>();
                        musicgroup.setMusicgroupartistCollection(emptyCollection);
                    } else {
                        musicgroup.getMusicgroupartistCollection().clear();
                    }
                    Collection <Musicgroupartist> mMusicGroupArtistCollection = musicgroup.getMusicgroupartistCollection();
                    
                    if (curMusicGroupArtist.isEmpty() == false) {
                        for (int curRow=0;curRow<curMusicGroupArtist.size();curRow++) {
                            Iterator artist_iter = art.iterator();
                            while (artist_iter.hasNext()) {
                                Artist cur_artist = (Artist) artist_iter.next();
                                if (cur_artist.getArtistid()== (long)curMusicGroupArtist.get(curRow)[1]) {
                                    Musicgroupartist curMusicGroupArtist = new Musicgroupartist();
                                    curMusicGroupArtist.setMusicgroupid(musicgroup);
                                    curMusicGroupArtist.setArtistid(cur_artist);
                                    //musicgroup.getMusicgroupid(), cur_artist.getArtistid());
                                    mMusicGroupArtistCollection.add(curMusicGroupArtist);
                                }
                            }
                        }
                    }
                    musicgroup.setMusicgroupartistCollection(mMusicGroupArtistCollection);                    
                    LYRA.e.persist(musicgroup);
                    

                    et.commit();
                    FinisEdit();
                    parentForm.RefreshMusicGroupTable();
                    // Αφού αποθηκεύσαμε κλείνουμε και τη φόρμα
                    parentForm.EnableDisableForm(true);
                    this.dispose();
                } catch (Exception ex) {
                    LYRA.e.clear();
                    if (et.isActive()) et.rollback();
                    ex.printStackTrace(System.out);
                    LYRA.MsgBox(ex.getMessage(), "Πρόβλημα");
                }
                
            }
        }
    }
    
    private void setChanges( boolean modified )
    {
        if (modified)
        {
            tbbModify.setEnabled(true);
            tbbExit.setEnabled(true);            
        }
        //Ένδειξη ύπαρξης/μη ύπαρξης αλλαγών
        bChanges= modified;        
    }
    
    private void Insertion() {

        //Δημιουργία νέου αντικειμένου artist της κλάσης Artist
        musicgroup = new Musicgroup();
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();
        
        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, musicgroup, org.jdesktop.beansbinding.ELProperty.create("${name}"), txtName, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, musicgroup, org.jdesktop.beansbinding.ELProperty.create("${dateformation}"), dtFormationDate, org.jdesktop.beansbinding.BeanProperty.create("date"));
        bindingGroup.addBinding(binding);
        
        bindingGroup.bind();
        
        setChanges(false);
        
        StartEdit();
        txtName.requestFocus();

    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.persistence.EntityManager LYRAPUEntityManager;
    private javax.swing.JButton btnAddMusicGroupArtist;
    private javax.swing.JButton btnAddSelectedArtistToMusicGroup;
    private javax.swing.JButton btnDeleteMusicGroupArtist;
    private org.jdesktop.swingx.JXDatePicker dtFormationDate;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JLabel lblAvailableArtists;
    private javax.swing.JToolBar tbarManage;
    private javax.swing.JButton tbbDelete;
    private javax.swing.JButton tbbExit;
    private javax.swing.JButton tbbInsert;
    private javax.swing.JButton tbbModify;
    private javax.swing.JTable tblAvailableArtists;
    private javax.swing.JTable tblMusicGroupArtists;
    private javax.swing.JTextField txtName;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
}
